import * as types from './user.mutationsType'
import * as methods from '../../api/user'
const user = {
  state: {
    navlist: [
      {id: 10031, name: 'HOME', routename: 'home'},
      {id: 10032, name: 'ARTICLES', routename: 'listpage'},
      {id: 10033, name: 'INTRODUCE', routename: 'DetailsPage'},
      {id: 10034, name: 'CONTACT ME', routename: 'aboutme'},
      {id: 10035, name: 'USER CENTER', routename: 'usercenter'}],
    userInfo: {}
  },
  mutations: {
    [types.NAVLIST] (state, navlist) {
      state.navlist = navlist
    },
    [types.USERINFO] (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    _setNavList ({commit}, navlist) {
      commit('NAVLIST', navlist)
    },
    _register ({commit}, data) {
      return new Promise((resolve, reject) => {
        methods.register(data)
          .then((res) => {
            resolve(res)
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    _login ({commit}, data) {
      return new Promise((resolve, reject) => {
        methods.login(data)
          .then((res) => {
            resolve(res)
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    _getUserInfo ({commit}, data) {
      return new Promise((resolve, reject) => {
        methods.getUserInfo(data)
          .then((res) => {
            if (res.data.status === 0) {
              this.commit('USERINFO', res.data.result)
            }
            resolve(res)
          })
          .catch((err) => {
            reject(err)
          })
      })
    }
  }
}
export default user
